
/* JS EXTENSION
 * setSelectMailTpl.js
 */


jQuery(document).ready(function(){
    flg_apm.setSelectMailTpl.init();
    flg_apm.setSelectMailTpl.initClicks();
    f=$('#do_sending_test');
//$(f).val('');
});


flg_apm.setSelectMailTpl=new flg_apm.setField('setSelectMailTpl','.c_setSelectMailTpl');


flg_apm.setSelectMailTpl.during_create=function(fi,obj){
	$.ajax({
		url: ajaxurl ,
		type: "POST",
		data: "subaction=getMailTpl&action=apm_extensions&entity=setSelectMailTpl",
		error: function(data){
			console.log(data);
		},
		success: function(data){
			flg_apm.setSelectMailTpl.ShowSelectMailTpl($.JSON.decode(data));
		}
	});
    return fi;
}
flg_apm.setSelectMailTpl.ShowSelectMailTpl = function(data){
	sel=$('#mail_compose_selecttpl_select');
    selected_value=$(sel).attr('selected_value');
    var str="<option value=''>--None--</option>";
    $.each(data,function(k,tpl){
        selec="";
        if(selected_value==tpl.ID){
            selec=" selected='selected' "
        }
        str+="<option value='"+tpl.ID+"' "+selec+" tpl-txt='"+tpl.post_name+"'>"+tpl.post_title+"</option>";
    });
    $(sel).html(str)
    $(sel).removeAttr('disabled');
}

flg_apm.setSelectMailTpl.postcreate=function(fi,obj){
}

flg_apm.setSelectMailTpl.replyMail=function(detailMail,action){
	console.debug(detailMail);
	console.debug(action);
	/*
	var titleMail = detailMail.subject;
	
	var tmp_bodyMail = detailMail.textPlain;	
	var bodyMail = '=============REPLY ABOVE THIS LINE=================='+
				   '<br/>The '+detailMail.date+', '+detailMail.fromName+' has written:<br/>';
	bodyMail += tmp_bodyMail.replace('\\n', '\\n >');
	
	$('#3module_information input#mailboxemail_subject').val(titleMail);
	$("#3module_information #mail_compose_rte_rte").wysiwyg("setContent", bodyMail);
	switch(action){
		case 'reply':
		break;
		case 'reply_all':
		break;
		case 'forward':
		break;
		
	}
	*/
}

flg_apm.setSelectMailTpl.initClicks=function(){
	
	$('.c_setSelectMailTpl button.select_tplMail').off('click').on('click',function(){
		var id_tplMail = $('.c_setSelectMailTpl #mail_compose_selecttpl_select').val();	
		flg_apm.setAlertPanel.addAlert_posAlertYBase('Loading','Loading the Email Template' ,'default',2000,595);
		$.ajax({
			url: ajaxurl ,
			type: "POST",
			data: "subaction=getMailTplDetail&action=apm_extensions&entity=setSelectMailTpl&ID_tplMail="+id_tplMail,
			error: function(data){
				console.log(data);
			},
			success: function(data){
				if(data){
					data = $.JSON.decode(data);
					if(data){
						
						$('#3module_information input#mailboxemail_subject').val(data[0].email_subject);
						$("#3module_information #mail_compose_rte_rte").wysiwyg("setContent", data[0].email_body);
					}
				}
			}
		});
	});
	
	$('.c_setSelectMailTpl button.manager_tplMail').off('click').on('click',function(){
		window.location = '/wp-admin/admin.php?page=15MAIL-ff_email_template';
	});
}
